package com.google.code.p2pxmppclient.executers.amule;


/**
 * 
 * @author eliosh
 * @version $Rev$
 * @lastautorcommit $Author$
 * @lastdatecommit $Date$
 */
public class ShowLogAmulecmdResponseTranslator implements AmulecmdResponseTranslator {

	@Override
	public String translate(String[] lines) {		
		StringBuffer sb = new StringBuffer();
		
		// first line blank, so we have a readable message :-)
		sb.append("\n");

		int currIndex = lines.length-1;
		int lineInserted = 0;
		// for(String line : lines){
		while(currIndex>=0 && lineInserted<20){
			String line = lines[currIndex--];
			String data = line.substring(24);
			if(discardLine(data)){
				continue;
			}else{
				sb.append(line.substring(3));
				sb.append("\n");
				lineInserted++;
			}
		}
		return sb.toString();
	}

	private boolean discardLine(String line){
		String[] phAr = {
			"Messaggio del server",
			"Accettata nuova",
			"Connessione al client",
			"Accesso consentito",
			"Hasher",
			"ThreadScheduler",
			"Connessione esterna chiusa"
		};
		boolean retVal = line.contains(" invio del file");
		for(String ph : phAr){
			if(line.length()>ph.length() && line.substring(0, ph.length()).equals(ph)){
				retVal = true;
			}
		}
		return retVal;
	}
}
